#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/7/5 14:14
   @File: SQLMontage.py
"""
import json
import os


# table_name ='fnd_client'
from CommonScripts.Logs import logger


def montage_sql_value(**kwargs):
    '''
    sql拼接
    '''
    table_parameter = kwargs.get('table')
    sql_list= kwargs.get('sql_list')
    # sql_list_source = kwargs.get('sql_list')
    # sql_list= {k.lower(): v for k, v in sql_list_source.items()}
    sql_value = f"("
    table_parameter = {key: sql_list[key] if key in sql_list else value for key, value in table_parameter.items()}
    values = []
    for value in table_parameter.values():
        if isinstance(value, str):
            values.append("'" + value + "'")
        else:
            values.append(str(value))
    output = ", ".join(values)
    sql = f'{sql_value}{output})'
    # print(sql.replace("None","null"))
    return sql.replace("None","null")

def get_sql_key(**kwargs):
    table_name = kwargs.get('table')
    sql_key = f"insert into {get_sql1(table=table_name).lower()} ("
    table_parameter = get_sql(table=table_name)
    keys = [str(key) for key in table_parameter.keys()]  # 将所有值转换为字符串类型
    output = ", ".join(keys)
    sql = f'{sql_key}{output}) values '
    return sql

def get_sql(**kwargs):
    '''
    读取sql语句
    '''
    table = kwargs.get('table')
    sql_list = read_file(table+'.json')
    for key, value in sql_list.items():
        paramer = {}
        for key_1, value_1 in value[0].items():
            keyLower = key_1.lower()
            paramer.update({keyLower:value_1})
        return paramer

def get_sql1(**kwargs):
    '''
    读取sql语句
    '''
    table = kwargs.get('table')
    sql_list = read_file(table+'.json')
    for key, value in sql_list.items():
        return key

def txt_write(path,content,model='a+'):
    Note = open(path, mode=model)
    Note.write(content)


def read_file(fileName):
    filepath = getFilePath(fileName=fileName)
    '''
     读取json文件，返回json数据
    '''''
    logger.info(f'filepath=={filepath}')
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
        # logger.info(f'讀取文件返回：{content}')
        return json.loads(content)


def getFilePath(fileName, path=r"E:\AutoTest\Project\金融反洗钱项目\web应用\DataTest"):
    fileName = fileName.strip()
    if '执行机系列对外' in fileName:
        return fileName
    else:
        for root, dirs, files in os.walk(path):
            for file in files:
                 # 获取文件所属目录
                # print(root)
                # 获取文件路径
                file = os.path.join(root, file)
                # print(os.path.join(root, file))
                if fileName in file:
                    # logger.info(f'文件路径：{file}')
                    return file

if __name__ == '__main__':
    # sql_list = {}
    table = get_sql(table='fnd_client')
    table_1 = get_sql1(table='fnd_client')
    print(table_1)
    # print(datetime.datetime.now())
    # for i in range(0,10):
    #     # sql_list = {'client_id':str(i),'client_name': '个人'+str(i)}
    #     sql_list = client_info()
    #     montage_sql_value(table=table, sql_list=sql_list)
    # print(datetime.datetime.now())
    # now = datetime.datetime.now()
    # formatted_datetime = now.strftime("%m%d%H")
    # print (formatted_datetime)
    # temp = '07052000000000'
    # print(temp[:14-2])

